package me.firstapp.service.weixin.product.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.weixin.product.Product;
import me.firstapp.module.weixin.product.ProductCategory;
import me.firstapp.module.weixin.product.Supplier;
import me.firstapp.repository.weixin.product.ProductCategoryRepository;
import me.firstapp.repository.weixin.product.ProductRepository;
import me.firstapp.repository.weixin.product.SupplierRepository;
import me.firstapp.service.weixin.product.ProductService;

@Service
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductRepository productRepository;

	@Autowired
	private ProductCategoryRepository productCategoryRepository;

	@Autowired
	private SupplierRepository supplierRepository;

	@Override
	public Page<Product> findProductPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType) {
		return productRepository.findProductPage(pageNo, pageSize, searchParams, sortParam, sortType);
	}

	@Override
	public Product findById(Long id) {
		return (Product) productRepository.getById(id);
	}

	@Override
	public Product executeAdd(Long productCategoryId, Long supplierId, String name, String imgUrl, String detailUrl,
			String outUrl, String unit, BigDecimal cost, BigDecimal startFreight, BigDecimal unitFreight,
			Integer limitUnit, BigDecimal fansPrice, BigDecimal baseVipPrice, BigDecimal highVipPrice,
			BigDecimal chiefBaseVipJinBi, BigDecimal chiefHighVipJinBi, BigDecimal chiefBaseVipPoint,
			BigDecimal chiefHighVipPoint, String description, String remark, Integer tuiJian,
			BigDecimal fansBuyGiveOtherJb, BigDecimal baseVipBuyGiveOtherJb, BigDecimal highVipBuyGiveOtherJb,
			Integer sort) {
		Supplier supplier = (Supplier) supplierRepository.getById(supplierId);
		if (null == supplier) {
			throw new ServiceException("error", "供应商不存在");
		}
		ProductCategory category = (ProductCategory) productCategoryRepository.getById(productCategoryId);
		if (null == category) {
			throw new ServiceException("error", "商品分类不存在");
		}
		Product product = new Product();
		product.setProductCategoryId(productCategoryId);
		product.setCategoryName(category.getName());
		product.setSupplierId(supplierId);
		product.setName(name);
		product.setImgUrl(imgUrl);
		product.setDetailUrl(detailUrl);
		product.setOutUrl(outUrl);
		product.setUnit(unit);
		product.setCost(cost);
		product.setStartFreight(startFreight);
		product.setUnitFreight(unitFreight);
		product.setLimitUnit(limitUnit);
		product.setFansPrice(fansPrice);
		product.setBaseVipPrice(baseVipPrice);
		product.setHighVipPrice(highVipPrice);
		product.setChiefBaseVipJinBi(chiefBaseVipJinBi);
		product.setChiefHighVipJinBi(chiefHighVipJinBi);
		product.setChiefBaseVipPoint(chiefBaseVipPoint);
		product.setChiefHighVipPoint(chiefHighVipPoint);
		product.setFansBuyGiveOtherJb(fansBuyGiveOtherJb);
		product.setBaseVipBuyGiveOtherJb(baseVipBuyGiveOtherJb);
		product.setHighVipBuyGiveOtherJb(highVipBuyGiveOtherJb);
		product.setStatus(Product.STATUS_OFF);
		product.setTimeAt(new Date());
		product.setDescription(description);
		product.setRemark(remark);
		product.setTuiJian(tuiJian);
		product.setSort(sort);
		product.setSaleCounts(0L);
		productRepository.save(product);
		return product;
	}

	@Override
	public Product executeUpdate(Long id, Long productCategoryId, Long supplierId, String name, String imgUrl,
			String detailUrl, String outUrl, String unit, BigDecimal cost, BigDecimal startFreight,
			BigDecimal unitFreight, Integer limitUnit, BigDecimal fansPrice, BigDecimal baseVipPrice,
			BigDecimal highVipPrice, BigDecimal chiefBaseVipJinBi, BigDecimal chiefHighVipJinBi,
			BigDecimal chiefBaseVipPoint, BigDecimal chiefHighVipPoint, String description, String remark,
			Integer status, Integer tuiJian, BigDecimal fansBuyGiveOtherJb, BigDecimal baseVipBuyGiveOtherJb,
			BigDecimal highVipBuyGiveOtherJb, Integer sort) {
		Product product = (Product) productRepository.getById(id);
		if (null == product) {
			throw new ServiceException(StatusHouse.STATUS_PRODUCT_NOT_EXIST);
		}
		if (null != productCategoryId) {
			ProductCategory category = (ProductCategory) productCategoryRepository.getById(productCategoryId);
			if (null == category) {
				throw new ServiceException("error", "商品分类不存在");
			}
			product.setProductCategoryId(productCategoryId);
			product.setCategoryName(category.getName());
		}
		if (null != supplierId) {
			Supplier supplier = (Supplier) supplierRepository.getById(supplierId);
			if (null == supplier) {
				throw new ServiceException("error", "供应商不存在");
			}
			product.setSupplierId(supplierId);
		}
		if (StrUtils.isNotNULL(name)) {
			product.setName(name);
		}
		if (StrUtils.isNotNULL(imgUrl)) {
			product.setImgUrl(imgUrl);
		}
		if (StrUtils.isNotNULL(detailUrl)) {
			product.setDetailUrl(detailUrl);
		}
		if (StrUtils.isNotNULL(outUrl)) {
			product.setOutUrl(outUrl);
		}
		if (StrUtils.isNotNULL(unit)) {
			product.setUnit(unit);
		}
		if (null != cost) {
			product.setCost(cost);
		}
		if (null != startFreight) {
			product.setStartFreight(startFreight);
		}
		if (null != unitFreight) {
			product.setUnitFreight(unitFreight);
		}
		if (null != limitUnit) {
			product.setLimitUnit(limitUnit);
		}
		if (null != fansPrice) {
			product.setFansPrice(fansPrice);
		}
		if (null != baseVipPrice) {
			product.setBaseVipPrice(baseVipPrice);
		}
		if (null != highVipPrice) {
			product.setHighVipPrice(highVipPrice);
		}
		if (null != chiefBaseVipJinBi) {
			product.setChiefBaseVipJinBi(chiefBaseVipJinBi);
		}
		if (null != chiefHighVipJinBi) {
			product.setChiefHighVipJinBi(chiefHighVipJinBi);
		}
		if (null != chiefBaseVipPoint) {
			product.setChiefBaseVipPoint(chiefBaseVipPoint);
		}
		if (null != chiefHighVipPoint) {
			product.setChiefHighVipPoint(chiefHighVipPoint);
		}
		if (null != fansBuyGiveOtherJb) {
			product.setFansBuyGiveOtherJb(fansBuyGiveOtherJb);
		}
		if (null != baseVipBuyGiveOtherJb) {
			product.setBaseVipBuyGiveOtherJb(baseVipBuyGiveOtherJb);
		}
		if (null != highVipBuyGiveOtherJb) {
			product.setHighVipBuyGiveOtherJb(highVipBuyGiveOtherJb);
		}
		if (StrUtils.isNotNULL(description)) {
			product.setDescription(description);
		}
		if (StrUtils.isNotNULL(remark)) {
			product.setRemark(remark);
		}
		if (null != status) {
			product.setStatus(status);
		}
		if (null != tuiJian) {
			product.setTuiJian(tuiJian);
		}
		if (null != sort) {
			product.setSort(sort);
		}
		productRepository.update(product);
		return product;
	}

	@Override
	public List<Product> findByCategoryId(Integer pageNo, Integer pageSize, Long productCategoryId) {
		return productRepository.findByCategoryId(pageNo, pageSize, productCategoryId);
	}

	@Override
	public List<Product> findByTuiJian() {
		return productRepository.findByTuiJian();
	}

	@Override
	public List<Product> findByKeyword(Integer pageNo, Integer pageSize, String keyword) {
		return productRepository.findByKeyword(pageNo, pageSize, keyword);
	}

}
